package com.mayisoftware.mayioa.business.plan.impl.serviceImpl;

import com.mayisoftware.mayioa.business.plan.api.constant.PlanCfgConstants;
import com.mayisoftware.mayioa.business.plan.api.domain.CheckRule;
import com.mayisoftware.mayioa.business.plan.api.domain.PlanConfig;
import com.mayisoftware.mayioa.business.plan.api.service.IPlanConfigService;
import com.mayisoftware.mayioa.business.plan.api.service.ObjectPropertyPredicate;
import com.mayisoftware.mayioa.business.plan.impl.dao.PlanConfigMapper;
import com.mayisoftware.mayioa.common.api.annotation.DataScope;
import com.mayisoftware.mayioa.common.api.enums.FilterMode;
import com.mayisoftware.mayioa.common.api.enums.RuleTarget;
import com.mayisoftware.mayioa.common.impl.support.Convert;
import com.mayisoftware.mayioa.common.impl.utils.StringUtils;
import com.mayisoftware.mayioa.platform.api.domain.SysUser;
import com.mayisoftware.mayioa.platform.impl.util.ShiroUtils;
import org.apache.commons.lang3.EnumUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * 计划配置 业务层处理
 * 
 *
 */
//@Service
public class PlanCheckServiceImpl
{

    /**
     * 通过批量过滤条件（codes） 过滤指定对象属性的值
     * @param dataSource 数据源集合
     * @param codes 过滤条件集合
     * @param express 过滤集合对象目标属性表达式
     * @param filterMode 过滤方式
     * @return 返回符合过滤条件codes的对象集合
     */
    public static List<SysUser> queryArray(List<SysUser> dataSource, List<String> codes,
                                           ObjectPropertyPredicate<SysUser> express, FilterMode filterMode)
    {
        List<SysUser> result = new ArrayList<>();
        if (dataSource == null)
        {
            return result;
        }
        if (codes == null || (long) codes.size() == 0)
        {
            return dataSource;
        }
        if (express == null)
        {
            return dataSource;
        }

        switch(filterMode)
        {
            case startsWith:{
                Predicate<SysUser> expressOr = f -> false;
                //过滤条件
                for (String code : codes)
                {
                    Predicate<SysUser> press = (w) -> express.getProperty(w).toString().startsWith(code);
                    expressOr = expressOr.or(press);
                }
                result = dataSource.stream().filter(expressOr).collect(Collectors.toList());
                break;
            }
            case equals:{
                Predicate<SysUser> expressOr = f -> false;
                for (String code : codes)
                {
                    Predicate<SysUser> press = (w) ->StringUtils.equals(express.getProperty(w).toString(),code);
                    expressOr = expressOr.or(press);
                }
                result = dataSource.stream().filter(expressOr).collect(Collectors.toList());
                break;
            }
            case notLess:
                break;
            case notGreater:
                break;
            case notLower:
                break;
            case contains:{
                result = dataSource.stream().filter(f -> codes.contains(express.getProperty(f).toString())).collect(Collectors.toList());
                break;
            }
            case containsAbove: {
                Predicate<SysUser> expressOr = f -> false;
                for (String code : codes)
                {
                    Predicate<SysUser> press = (w) ->StringUtils.equals(express.getProperty(w).toString(),code);
                    expressOr = expressOr.or(press);
                }
                result = dataSource.stream().filter(expressOr).collect(Collectors.toList());
                break;
            }
        }

        return result;
    }

    private static ObjectPropertyPredicate<SysUser> getObj(RuleTarget ruleTarget)
    {
        ObjectPropertyPredicate<SysUser> express = null;
        switch(ruleTarget){
            case PROJCET_ROLE:{
                express = SysUser::getProjectRole;
                break;
            }
            case USER_POST:{
                express = SysUser::getRsv1;
                break;
            }
            case REGION:{
                express = SysUser::getRegion;
                break;
            }
            case WORK_YEAR:{
                express = SysUser::getWorkTime;
                break;
            }
            case JOIN_YEAR:{
                express = SysUser::getJoinTime;
                break;
            }
            case ACHIEVEMENTS:{
                express = SysUser::getProjectRole;
                break;
            }
            case CONTRIBUTION:{
                express = SysUser::getProjectRole;
                break;
            }
            case PLAN_NUM:{
                express = SysUser::getProjectRole;
                break;
            }
        }
        return express;
    }

//    public static void main(String[] args){
//        List<CheckRule> ruleList = new ArrayList<>();
//        CheckRule checkRule = new CheckRule();
//        checkRule.setRuleTargetCls("SysUser");
//        checkRule.setRuleTargetProp("getHwLevel");
//        checkRule.setRuleFilter(FilterMode.containsAbove.toString());
//        checkRule.setRuleValue("61");
//        ruleList.add(checkRule);
//
//        List<SysUser> userList = new ArrayList<>();
//        SysUser user = new SysUser();
//        user.setRegion("西安");
//        user.setHwLevel("71");
//        user.setProjectRole("PM");
//
//        Calendar cal = Calendar.getInstance();
//        cal.set(2017,7,19);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        System.out.println(sdf.format(cal.getTime()));
//        user.setJoinTime(cal.getTime());
//        userList.add(user);
//
//        for (CheckRule rule : ruleList)
//        {
//
//            // 用户信息过滤
//            if(EnumUtils.getEnum(RuleTarget.class,rule.getRuleTargetCls()).obj().equals("SysUser"))
//            {
//                List<String> filterList = new ArrayList<>();
//                filterList.add(rule.getRuleValue());
//
//                userList = queryArray(userList, filterList,
//                        getObj(EnumUtils.getEnum(RuleTarget.class,rule.getRuleTargetCls())), EnumUtils.getEnum(FilterMode.class,rule.getRuleFilter()));
//                System.out.println(userList.size());
//            }
//        }
//
//
//    }
    
}
